package 剑指offer;

import java.util.ArrayList;

/**
 * Created by lenovo】 on 2017/3/10.
 */
public class GetLeastNumbersFromArray {
    public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        int len = input.length;
        int start = len - 1;
        int end = len - k;
        if (end < 0) {
            return arrayList;
        }
        BubbleSort(input ,len ,  k);
        for (int i = start; i >= end; i --) {
            arrayList.add(input[i]);
        }
        return arrayList;
    }

    public void BubbleSort(int [] list , int len, int k) {
        for (int i = 1 ; i < len ; i++) {
            for (int j = 0 ; j< len - i; j ++) {
                if (i == k+1) {
                    break;
                }
                if (list[j + 1] > list[j]) {
                    int temp = list[j];
                    list[j] = list[j + 1];
                    list[j + 1] = temp;
                }
            }
        }
    }
}
